package com.example.mythealthapp.food_list

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.example.mythealthapp.R
import com.example.mythealthapp.bean.FoodBean

class InfoListAdapter(var context: Context, val mDatas: List<FoodBean>) :
    BaseAdapter() {

    //  得到数量
    override fun getCount(): Int {
        return mDatas.size
    }
    //返回指定位置对应的数据
    override fun getItem(position: Int): Any {
        return mDatas[position]
    }
    // 返回指定位置所对应的id
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
        var cv = convertView
        val holder: ViewHolder?
        if (cv == null) {
            cv = LayoutInflater.from(context)
                .inflate(R.layout.item_infolist_lv, null) //将布局转换成view对象的方法
            holder = ViewHolder(cv)
            cv.tag = holder
        } else {
            holder = cv.tag as ViewHolder
        }
        //加载控件显示的内容
        //获取集合指定位置的数据
        val foodBean: FoodBean = mDatas[position]
        holder.titleTv.text = foodBean.title
        holder.notTv.text = "不可匹配:" + foodBean.notmatch
        holder.iv.setImageResource(foodBean.picId)
        return cv
    }

    internal inner class ViewHolder(view: View) {
        var iv: ImageView = view.findViewById(R.id.item_info_iv)
        var titleTv: TextView = view.findViewById(R.id.item_info_tv_title)
        var notTv: TextView = view.findViewById(R.id.item_info_tv_notmatch)
    }

}
